<template>
	<view class="about-container">
		
		<!-- Tab Buttons -->
		<div class="tabs">
			<button @click="switchCategory(0)" :class="{ active: cat === 0 }">官方</button>
			<button @click="switchCategory(1)" :class="{ active: cat === 1 }">民间</button>
		</div>
	
		<!-- 图片区域 -->
		<view class="image-section">
			<image v-if="imageUrl" :src="imageUrl" @error="handleImageError" class="image" mode="aspectFit"></image>
		</view>
		
		<!-- 标题区域 -->
		<view class="title-section">
			<view class="title">
				{{ title }}
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="content-section">
			<view class="content">
				<view class="text" v-html="formattedContent">
				</view>

				<!-- 上下文链接 -->
				<view class="context-links">
					<view v-if="prevArticle.id" class="link">
						<view @click="navigateTo(`/pages/fun/fun?id=${prevArticle.id}`)" class="link-content">
							<u-icon name="play-left-fill" color="#666" size="18"></u-icon>
							<view>{{ prevArticle.title }}</view>
						</view>
					</view>
					<view v-else class="link no-more">
						没有更多了
					</view>
					<view v-if="nextArticle.id" class="link">
						<view @click="navigateTo(`/pages/fun/fun?id=${nextArticle.id}`)" class="link-content">
							<view>{{ nextArticle.title }}</view>
							<u-icon name="play-right-fill" color="#666" size="18"></u-icon>
						</view>
					</view>
					<view v-else class="link no-more">
						没有更多了
					</view>
				</view>

				<!-- 返回首页按钮 -->
				<view class="back-home" @click="goToHome">
					返回首页
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getFunById,
		getFunNextById,
		getFunPreById
	} from '@/api/system/fun';

	export default {
		data() {
			return {
				title: '每日膳食-如何保持健康的饮食习惯',
				imageUrl: '/static/images/sci.png',
				content: "每日膳食-如何保持健康的饮食习惯",
				cat: 0, // Default category is official
				prevArticle: {},
				nextArticle: {}
			};
		},
		computed: {
			formattedContent() {
				return this.content;
			}
		},
		mounted() {
			let id = this.$route.query.id;
			if (id) {
				this.getFun(id);
			}
		},
		methods: {
			handleImageError(event) {
				console.error('图片加载失败:', event);
			},
			getFun(id) {
				getFunById(id).then(res => {
					this.title = res.data.title;
					this.imageUrl = res.data.cover;
					this.content = res.data.content;
					this.cat=res.data.cat;
					this.getPrevAndNextFun(id);
				}).catch(err => {
					console.error('获取膳食数据失败:', err);
				});
			},
			getPrevAndNextFun(id) {
				getFunPreById(id).then(res => {
					this.prevArticle = res.data || {};
				}).catch(err => {
					console.error('获取上一条消息失败:', err);
					this.prevArticle = {};
				});
				getFunNextById(id).then(res => {
					this.nextArticle = res.data || {};
				}).catch(err => {
					console.error('获取下一条消息失败:', err);
					this.nextArticle = {};
				});
			},
			navigateTo(url) {
				console.log("跳转到" + url);
				if (url.startsWith('/pages/home/home')) {
					this.goToHome();
				} else {
					uni.navigateTo({
						url
					});
				}
			},
			goToHome() {
				console.log("跳转到首页");
				uni.switchTab({
					url: '/pages/index'
				});
			},
			switchCategory(newCat) {
				this.cat = newCat;
				this.currentPage = 1;
				const url = '/pages/fun/list';
				uni.switchTab({
					url: url
				});
			}
		}
	};
</script>

<style scoped>
	.uni-tabbar-bottom { display: block; }
	.about-container {
		background-color: #fff;
		padding: 20px;
	}

	.back-home {
		font-size: 16px;
		color: #007aff;
		text-align: center;
		padding: 10px;
		cursor: pointer;
		margin-bottom: 20px;
	}

	.back-home:hover {
		color: #0056b3;
	}

	.title-section {
		margin-bottom: 20px;
	}
	
	.title {
		font-size: 24px;
		font-weight: bold;
		text-align: left;
	}

	.image-section {
		margin-bottom: 20px;
	}

	.image {
		width: 100%;
		max-width: calc(100vw - 40px);
		max-height: 300px;
		border-radius: 25px;
	}

	.content-section {
		margin-bottom: 20px;
	}

	.text {
		font-size: 16px;
		line-height: 1.5;
		margin-bottom: 10px;
	}

	.context-links {
		display: flex;
		justify-content: space-between;
		margin-top: 40px;
	}

	.link {
		flex: 1;
		display: flex;
		align-items: center;
		cursor: pointer;
	}

	.link-content {
		display: flex;
		justify-content: space-between;
		width: 100%;
	}

	.link:hover {
		background-color: #d3d3d3;
	}

	.no-more {
		color: grey;
	}

	.tabs {
		display: flex;
		width: 100%;
		margin-bottom: 20px;
	}

	.tabs button {
		flex: 1;
		background: none;
		border: none;
		font-size: 16px;
		padding: 0;
		cursor: pointer;
		transition: color 0.3s ease;
		text-align: center;
	}

	.tabs button.active {
		font-weight: bold;
		color: #007BFF;
		border-bottom: 2px solid #007BFF;
		background: #fff;
	}
</style>
